package com.allqj.sfx.ddd.member.infrastructure.converter;

import com.allqj.sfx.ddd.member.domain.entity.flow.FlowDef;
import com.allqj.sfx.ddd.member.infrastructure.dao.FlowDefDAO;
import com.allqj.sfx.ddd.member.infrastructure.po.FlowDefDO;
import com.allqj.sfx.infrastructure.converter.DataConverter;
import org.springframework.beans.BeanUtils;

import java.util.Optional;

/**
 * @author sjw
 * @className FlowDefConverter
 * @date 2022/7/20 15:37
 * @description: FlowDef&FlowDefDO转换器
 */
public class FlowDefConverter implements DataConverter<FlowDef, FlowDefDO, FlowDefDAO> {

    // 转换器实例
    public static FlowDefConverter INSTANCE = new FlowDefConverter();


    @Override
    public FlowDef do2Entity(FlowDefDO flowDefDO) {
        if (flowDefDO == null)
            return null;
        FlowDef flowDef = new FlowDef();
        BeanUtils.copyProperties(flowDefDO, flowDef);
        return flowDef;
    }

    @Override
    public FlowDefDO entity2Do(FlowDef flowDef) {
        if (flowDef == null) return null;
        FlowDefDO flowDefDO = new FlowDefDO();
        BeanUtils.copyProperties(flowDef, flowDefDO);
        return flowDefDO;
    }

    @Override
    public FlowDefDO entity2Do(FlowDef flowDef, FlowDefDAO flowDefDAO) {
        if (flowDef == null) return null;
        if (flowDef.getId() == null) {
            return entity2Do(flowDef);
        }
        Optional<FlowDefDO> flowDefDOOptional = flowDefDAO.findById(flowDef.getId());
        FlowDefDO flowDefDO = null;
        if (flowDefDOOptional.isPresent()) {
            flowDefDO = flowDefDOOptional.get();
            flowDefDO.setModuleId(flowDef.getModuleId());
            flowDefDO.setFunctionId(flowDef.getFunctionId());
            flowDefDO.setRemark(flowDef.getRemark());
            flowDefDO.setStatus(flowDef.getStatus());
        }
        return flowDefDO;
    }
}
